package com.lemon.boot.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lemon.boot.common.enums.WebTypeEnum;
import com.lemon.boot.dto.GeneratorTableDTO;
import com.lemon.boot.po.GeneratorTablePO;
import com.lemon.boot.service.IGeneratorTableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Map;

import static java.util.stream.Collectors.toMap;

/**
 * @author 李猛
 * @datetime 2024/2/26 14:00
 * @description 类对象
 */
@Log4j2
@Api(tags = "代码生成接口")
@RestController
@RequestMapping("generatorTable")
@PreAuthorize("hasRole('ROLE_SUPER')")
@ConditionalOnExpression("'${spring.profiles.active}'.equals('dev') ? true : false")
public class GeneratorTableController {
    @Resource
    private IGeneratorTableService iGeneratorTableService;

    /**
     * 添加单条数据
     *
     * @param generatorTablePO
     * @return
     */
    @ApiOperation(value = "添加单条数据")
    @PostMapping("add")
    public String add(@RequestBody GeneratorTablePO generatorTablePO) {
        return iGeneratorTableService.add(generatorTablePO);
    }

    /**
     * 根据ID删除数据
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据ID删除数据")
    @DeleteMapping("del/{id}")
    public String del(@PathVariable("id") Long id) {
        return iGeneratorTableService.del(id);
    }

    /**
     * 根据ID查询数据
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据ID查询数据")
    @GetMapping("get/{id}")
    public GeneratorTablePO get(@PathVariable("id") Long id) {
        return iGeneratorTableService.get(id);
    }

    /**
     * 根据实体类查询分页数据
     *
     * @param generatorTableDTO
     * @return
     */
    @ApiOperation(value = "根据实体类查询分页数据")
    @GetMapping("page")
    public Page<?> page(GeneratorTableDTO generatorTableDTO) {
        return iGeneratorTableService.page(generatorTableDTO);
    }

    /**
     * 获取所有数据库表
     *
     * @return
     */
    @ApiOperation(value = "获取所有数据库表")
    @GetMapping("getAllTable")
    public Map<String, Boolean> getAllTable() {
        return iGeneratorTableService.getAllTable();
    }

    /**
     * 获取生成页面的类型
     *
     * @return
     */
    @ApiOperation(value = "获取生成页面的类型")
    @GetMapping("getWebType")
    public Map<String, String> getWebType() {
        return Arrays.stream(WebTypeEnum.values()).collect(toMap(WebTypeEnum::getType, WebTypeEnum::getInfo));
    }
}
